<taglib name="Common\TagLib\Sohtml"/>
<div class="panel crm order-edit-page">
    <div style="width:450px; float:left;">
        <fieldset>
            <legend>筛选商品</legend>
            <table width="100%">
                <tr><td>分类：<dbhelper:catalogselect extags='PRODUCT-CATALOG' id="search-product-catalog" siteid="{$data.siteID}" /></td><td align="right">品名：<input type="text" id="search-product-name" placeholder="商品名称" /></td></td></tr>
                <tr><td colspan="2" align="right"><button class="btn-search" id="btn-search-product" data-search-url="{:U('Product/product_list_json')}">搜索商品</button></td></td></tr>
            </table>
            <div style="max-height:186px; overflow-y: auto" id="search-product-list">
                <table class="table-grid">
                    <tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>品名</th><th>价格</th><th>库存</th><th>操作</th></td></tr>
                    <!--<tr><td>1</td><td>商品1</td><td>12.00</td><td>30</td><td class="no-padding"><a href="#" class="button">查看</a><a href="#" class="button">介绍</a></td></td></tr>-->
                </table>
            </div>
        </fieldset>    
    </div>
    <div style="width:300px; float:left;">
        <form action="{:U('Product/internal_edit_save')}" method="post">
            <table class="table-form">
                <tr>
                    <th>商品名称：</th><td>
                    <input type="text" name="productName" readonly="readonly" placeholder="禁止编辑" />
                </td></tr>
                <tr>
                    <th>商品价格：</th><td>
                    <input type="text" name="price" readonly="readonly" placeholder="禁止编辑" />
                </td></tr>
                <tr>
                    <th>商品库存：</th><td>
                    <input type="text" name="oldStock" readonly="readonly" placeholder="禁止编辑" />
                </td></tr>
                <tr>
                    <th>出库数量：</th><td>
                    <input type="text" name="number" size="8" placeholder="出库数量" /> <span id="stocktip" style="color:#F00;"></span>
                </td></tr>
                <tr>
                    <th>剩余库存：</th><td>
                    <input type="text" name="stock" readonly="readonly" placeholder="禁止编辑" />
                </td></tr>
                <tr>
                    <th>出库备注：</th><td>
                    <input type="text" name="remark" size="24" placeholder="出库备注" />
                </td></tr>
                <tr>
                    <th>经手人：</th><td>
                    <input type="text" placeholder="经手人" value="{$data.userName}" readonly="readonly" />
                </td></tr>
                <tr>
                    <th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><td>                    
                <if condition="$upr['内部出库']"><button type="submit">保存</button></if><button type="reset">重置</button>
                </td></tr>
            </table>
            <input type='hidden' name='productID' />
            <input type='hidden' name='stock' />
        </form>
    </div>
    <div style="clear:both;"></div>
    <input type="hidden" id="ie-product-getdata-url" value="{:U('Product/product_item_json','id=varid')}" />
    <input type='hidden' name='siteID' value='{$data.siteID}'/>
</div>
<script type="text/javascript">
    resize_dialog(800);
    // 选择商品 2014-12-18 by sutroon
    $('#btn-search-product').click(function () {
        var cat = $('#search-product-catalog').val();
        var name = $('#search-product-name').val();
        load_product_list(cat, name);
    });
    load_product_list('', '');
    // 动态加载商品列表 2014-12-18 by sutroon
    function load_product_list(cat, name) {
        var siteID = $('input[name="siteID"]').val();
        $.post($('#btn-search-product').data('searchUrl'), {'cat': cat, 'name': name, 'limit': 50, 'siteID': siteID}, function (data) {
            var str = '<table class="table-grid"><tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>品名</th><th>价格</th><th>库存</th><th>操作</th></td></tr>';
            if (!data) {
                $('#search-product-list').html(str + '</table>');
                resize_dialog();
                return;
            }
            for (var i in data) {
                str += '<tr data-id="' + data[i].id + '"><td>' + (i * 1 + 1) + '</td><td class="name">' + data[i].name + '</td><td class="price">' + data[i].price + '</td><td class="stock">' + data[i].stock + '</td><td class="no-padding"><a href="#" class="button lnk-view">选择</a></td></td></tr>';
            }
            str += '</table>';
            $('#search-product-list').html(str);
            config_button();
            resize_dialog();
        });
    }
    // 按钮点击-查看商品
    $('#search-product-list').delegate('.lnk-view', 'click', function () {
        var $tr = $(this).parent().parent();
        $('input[name="productID"]').val($tr.data('id'));
        $('input[name="productName"]').val($tr.find('.name').text());
        $('input[name="oldStock"]').val($tr.find('.stock').text());
        $('input[name="price"]').val($tr.find('.price').text());
        $('input[name="number"]').val('');
        $('input[name="stock"]').val('');
        return false;
    });

    // 出库数量检查 2014-12-23 by sutroon
    $('input[name="number"]').blur(function () {
        var stock = $('input[name="oldStock"]').val();
        var number = $(this).val();
        if (!number) {
            $('#stocktip').text('出库数量无效!');
            $('input[name="stock"]').val('');
            return;
        }
        stock = stock - number;
        $('input[name="stock"]').val(stock);
        $('#stocktip').text(stock < 0 ? '出库数量超过库存!' : '');
    });
</script>